/*********************************************************************************
Description: This is a main function file
Author: SA12226114(Xin Zhu), SA12226139(Dan Su)
Date: 2012-11 

Copyright (c) 2012-2022 School of Software Engineering of USTC, All rights reserved.

**********************************************************************************/



#include "dbclient.h"




/*****************************************************************
Function Name: main
Description:
Parameter:  

@argc: argument count
@argv: argument vector

Author: 
Date:  
******************************************************************/
int main(int argc, char** argv)
{
    int sockfd;
    char recvline[MAXLINE], sendline[CMDLINE_LEN];
    char test[10];
    struct sockaddr_in servaddr;

    /* check whether argument count is right */
    if(argc != 2)
    {
        printf("usage: ./client <ipaddress>\n");
        
        return 0;
    }
    
    util_menu();
    
    /* receive command and send it to server. */
    while(1)
    {
        
        /* generate a new socket for client */
        if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        {
            printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
            
            return 0;
        }
        
        /* initialize structure sockaddr_in with protocol family, port number, ip */
        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(10000);
            
        if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
        {
            printf("inet_pton error for %s\n",argv[1]);
            
            return 0;
        }

        /* connect server using socket id */
        if(connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
        {
            printf("connect error: %s(errno: %d)\n",strerror(errno), errno);
            
            return 0;
        }

        
        printf(">>>");
        util_getcommandline(sendline, CMDLINE_LEN);
        
        if(send(sockfd, sendline, strlen(sendline), 0) < 0)
        {
            printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
            
            return 0 ;
        }

        close(sockfd);

    }

    

    return 0;
}
